package com.shanpin.shebao.admin.module.finance;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shanpin.shebao.admin.module.city.CityCacheService;
import com.shanpin.shebao.basedb.model.CityConfig;
import com.shanpin.shebao.model.report.DateWithCityAmount;
import com.shanpin.shebao.model.report.MonthAmount;

@Service
public class FinanceService {

	@Autowired
	private FinanceDao financeDao;
	@Autowired
	private CityCacheService cityCacheService;
	
	/**
	 * 加载月份流水汇总的数据
	 * @return
	 */
	public List<MonthAmount> loadMonthTurnover(boolean finance) {
		return financeDao.loadMonthTurnover(finance);
	}

	protected List<Integer> queryMonth() {
		return financeDao.queryMonth();
	}

	protected List<Integer> queryMonthOrderNums() {
		return financeDao.queryMonthOrderNums();
	}
	
	/**
	 * 月份下的各城市的月流水
	 * @param month
	 * @return
	 */
	public List<CityMonthResp> loadMonthTurnover(int month, String city_code, boolean finance) {
		 List<MonthAmount> list = financeDao.loadCityMonthTurnover(month, city_code, finance);
		 List<CityMonthResp> respList = new ArrayList<>(list.size());
		 MonthAmount ma = null;
		 for (Iterator<MonthAmount> it = list.iterator(); it.hasNext();) {
			 ma = it.next();
			 respList.add(CityMonthResp.valueOf(ma, 
					 cityCacheService.get(ma.getCity_code()).getCity_name()));
		 }
		 return respList;
	}
	
	
	public List<Integer> loadMonths(boolean finance) {
		return financeDao.loadMonths(finance);
	}
	
	public List<Integer> loadCityCodes(int month, boolean finanace) {
		return financeDao.loadCityCodes(month, finanace);
	}
	

	/**
	 * 月份下的各城市的月流水
	 * @param month
	 * @return
	 */
	public List<MonthExport> loadMonthTurnoverExport(int month, String city_code, boolean finance) {
		 List<MonthAmount> list = financeDao.loadCityMonthTurnover(month, city_code, finance);
		 List<MonthExport> respList = new ArrayList<>(list.size());
		 MonthAmount ma = null;
		 for (Iterator<MonthAmount> it = list.iterator(); it.hasNext();) {
			 ma = it.next();
			 respList.add(MonthExport.valueOf(ma, 
					 cityCacheService.get(ma.getCity_code()).getCity_name()));
		 }
		 return respList;
	}
	
	
	 Map<String, Collection<?>> addExistCitys(int month, boolean finanace) {
		 List<Integer> cityCodeList = loadCityCodes(month, finanace);
		 List<CityConfig> cityList = new ArrayList<>(cityCodeList.size());
		 TreeSet<CityConfig> pCityList = cityCacheService.cityCode2Exist(cityCodeList, cityList);
		 
		 Map<String, Collection<?>> citys = new HashMap<>(4);
		 citys.put("cityList", cityList);
		 citys.put("pCityList", pCityList);
		 return citys;
	}
	
	
	

	/**
	 * 加载日期内的城市流水
	 * @param date
	 * @param city_code
	 * @return
	 */
	public List<DateCityResp> loadDateTurnover(String date, String city_code) {
		List<DateWithCityAmount> list = null;
		if (StringUtils.isNotBlank(date) && StringUtils.isNotBlank(city_code)) {
			list = financeDao.queryDateCityTurnover(date, city_code);
			
		} else if (StringUtils.isNotBlank(city_code)) {
			list = financeDao.queryDateCityTurnover(city_code);
			
		} else if (StringUtils.isNotBlank(date)) {
			list = financeDao.queryDateTurnover(date);
		}
		List<DateCityResp> respList = new ArrayList<>(list.size());
		DateWithCityAmount dc = null;
		for (Iterator<DateWithCityAmount> it = list.iterator(); it.hasNext();) {
			dc = it.next();
			respList.add(DateCityResp.valueOf(dc, 
					cityCacheService.get(dc.getCity_code()).getCity_name()));
		}
		return respList;
	}
	

	public List<DateCityExport> loadDateCityExport(String date, String city_code) {
		List<DateWithCityAmount> list = null;
		if (StringUtils.isNotBlank(date) && StringUtils.isNotBlank(city_code)) {
			list = financeDao.queryDateCityTurnover(date, city_code);
			
		} else if (StringUtils.isNotBlank(city_code)) {
			list = financeDao.queryDateCityTurnover(city_code);
			
		} else if (StringUtils.isNotBlank(date)) {
			list = financeDao.queryDateTurnover(date);
		}
		List<DateCityExport> respList = new ArrayList<>(list.size());
		DateWithCityAmount dc = null;
		for (Iterator<DateWithCityAmount> it = list.iterator(); it.hasNext();) {
			dc = it.next();
			respList.add(DateCityExport.valueOf(dc, 
					cityCacheService.get(dc.getCity_code()).getCity_name()));
		}
		return respList;
	}
	
	/**
	 * 指定月内的每天的订单数列表
	 * @param yyyyMM	201609
	 * @return
	 */
	List<Integer> queryDateOrderNum(String yyyyMM) {
		return financeDao.queryDateOrderNum(yyyyMM+"01", yyyyMM+"31");
	}
	
	Map<String, Object> queryUserReport(String date) {
		DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd");
		String beginDate = DateTime.parse(date, format).plusDays(-32).toString("yyyy-MM-dd");
		List<Map<String, Object>> urList = financeDao.queryUserReport(beginDate, date);

		StringBuilder b = new StringBuilder(396);
		b.append("[");
		List<String> regList = new ArrayList<String>(34);
		List<String> loginNumList = new ArrayList<String>(34);
		List<String> loginUidList = new ArrayList<String>(34);
		for (Map<String, Object> urMap : urList) {
			b.append("'"+urMap.get("gen_date").toString() + "',");
			regList.add(urMap.get("reg_num").toString());
			loginNumList.add(urMap.get("login_num").toString());
			loginUidList.add(urMap.get("login_uid").toString());
		}
		Map<String, Object> map = new HashMap<>();
		map.put("dateList", b.deleteCharAt(b.length()-1).append("]").toString());
		map.put("regList", regList);
		map.put("loginNumList", loginNumList);
		map.put("loginUidList", loginUidList);
		return map;
	}
	
}
